package com.example.keeppwd2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Message;

public abstract class MyBlueTooth implements Runnable {

	private static String theDeviceId = null;
	private static BluetoothSocket socket = null;
	static long timeout = 0;

	public static BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	MyHandler mHandler;
	String msg;
	String deviceId;
	String errMsg;
	boolean isReadRes;
	String resStr;

	public abstract void OnSuccessCallBack(String resStr);

	public abstract void OnErrorCallBack(String msg);

	public void sendMsg(String msg, String deviceId, boolean isReadRes) {
		mHandler = new MyHandler() {

			@Override
			public void OnSuccess() {
				OnSuccessCallBack(resStr);
			}

			@Override
			public void OnFinish() {

			}

			@Override
			public void OnError() {
				OnErrorCallBack(errMsg);
			}
		};
		this.msg = msg;
		this.isReadRes = isReadRes;
		this.deviceId = deviceId;
		new Thread(this).start();
	}

	private static boolean checkConn(String deviceId, String checkConnStr) {
		boolean res = false;
		try {
			BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceId);
			BluetoothSocket so = device
					.createRfcommSocketToServiceRecord(UUID.fromString("29913A2D-EB93-40cf-BBB8-DEEE26452197"));
			so.connect();

			OutputStream os = so.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(checkConnStr);
			InputStream ins = so.getInputStream();
			DataInputStream dins = new DataInputStream(ins);
			String resStr = dins.readUTF();
			if ("1".equals(resStr)) {
				res = true;
			}
			so.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	private boolean send() {
		boolean res = false;
		try {
			long time = (new Date()).getTime();
			if (socket == null || !deviceId.equals(theDeviceId) || !socket.isConnected() || time - timeout > 180000) {
				if (socket != null) {
					try {
						socket.close();
					} catch (Exception e2) {
					}
				}
				theDeviceId = deviceId;
				BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(theDeviceId);
				socket = device
						.createRfcommSocketToServiceRecord(UUID.fromString("29913A2D-EB93-40cf-BBB8-DEEE26452197"));
				socket.connect();
			}
			timeout = time;
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
			InputStream ins = socket.getInputStream();
			DataInputStream dins = new DataInputStream(ins);
			resStr = dins.readUTF();
			res = true;
		} catch (Exception ex) {
			socket = null;
			errMsg = ex.getMessage();
		}
		return res;
	}

	public void run() {
		Message msg = null;
		for(int i=0;i<3;i++){
			if(send()) {
				msg = mHandler.obtainMessage(MyHandler.MessType_Succe);
				break;
			}
			if(i==2){
				break;
			}
			try {
				Thread.sleep(500*(i+1));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if (msg == null) {
			msg = mHandler.obtainMessage(MyHandler.MessType_Error);
		}
		mHandler.sendMessage(msg);
	}

}
